package cn.wangqi.personal.wxpay.util.webconfig;

/**
 * Created by admin on 2017/8/12.
 */

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Stream on 2015/11/25.
 */
public class NativePay {
    private static Logger logger = LoggerFactory.getLogger(NativePay.class);

    private static String access_token = null;
    private static long request_access_token_time = 0;

    private static String jsapi_ticket = null;
    private static long request_jsapi_ticket_time = 0;


    public enum trade_type {
        NATIVE, JSAPI, APP;
    }
    /**
     * 生成扫描支付模式一URL
     */

    /**
     * 生成直接支付url，支付url有效期为2小时,模式二
     */
    public String GetPayUrl(String out_trade_no, String total_fee, String subject) throws Exception {
        logger.info("Native pay mode 2 url is producing...");

        Map data = new HashMap();
        data.put("body", subject);// 商品描述
        data.put("attach", subject);// 附加数据
        //data.put("out_trade_no", WxPayApi.GenerateOutTradeNo());// 随机字符串
        data.put("out_trade_no", out_trade_no);
        data.put("total_fee", total_fee);// 总金额

        data.put("goods_tag", subject);// 商品标记
        data.put("trade_type", "NATIVE");// 交易类型
        data.put("product_id", out_trade_no);// 商品ID

        Map result = WxPayApi.UnifiedOrder(data);// 调用统一下单接口

        if (result.get("code_url")!=null) {
            String url = result.get("code_url").toString();// 获得统一下单接口返回的二维码链接
            logger.info("Get native pay mode 2 url : " + url);
            return url;
        } else {
            throw new RuntimeException("" + result.get("err_code_des"));
        }

    }
}

